package tr.edu.yildiz.ce.dap.appagent.util;

import java.math.BigInteger;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import tr.edu.yildiz.ce.dap.appagent.model.Actions;
import tr.edu.yildiz.ce.dap.appagent.model.App;
import tr.edu.yildiz.ce.dap.appagent.model.AppDefinition;
import tr.edu.yildiz.ce.dap.appagent.model.AppList;
import tr.edu.yildiz.ce.dap.appagent.model.Component;
import tr.edu.yildiz.ce.dap.appagent.model.ComponentList;
import tr.edu.yildiz.ce.dap.appagent.model.Criteria;
import tr.edu.yildiz.ce.dap.appagent.model.CriteriaList;
import tr.edu.yildiz.ce.dap.appagent.model.DapMessage;

public class AppAgentHandler extends BasicHandler {
	
	App currentApp;
	Actions currentActions;
	AppDefinition currentAppDefinition;
	AppList currentAppList;
	Component currentComponent;
	ComponentList currentComponentList;
	Criteria currentCriteria;
	CriteriaList currentCriteriaList;
	DapMessage currentDapMessage;
	
	
	String currentValue;
	boolean currentElement;

	@Override
	public void startElement(String namespaceURI, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (qName.equalsIgnoreCase(Constants.DAP_MESSAGE)){
			currentDapMessage = new DapMessage();		
			currentDapMessage.init(attributes);
		}else if (qName.equalsIgnoreCase(Constants.COMPONENT_LIST)){
			currentComponentList = new ComponentList();
			currentComponentList.init(attributes);
			if (currentAppDefinition != null)
				currentAppDefinition.setComponentList(currentComponentList);
			else
				currentDapMessage.setComponentList(currentComponentList);
		}if (qName.equalsIgnoreCase(Constants.COMPONENT)){
			currentComponent = new Component();
			currentComponent.init(attributes);
			currentComponentList.getComponent().add(currentComponent);
		}else if (qName.equalsIgnoreCase(Constants.ACTIONS)){
			currentActions = new Actions();
			currentActions.init(attributes);
			currentComponent.setActions(currentActions);
		}else if (qName.equalsIgnoreCase(Constants.APP_LIST)){
			currentAppList = new AppList();
			currentAppList.init(attributes);
			currentDapMessage.setAppList(currentAppList);
		}else if (qName.equalsIgnoreCase(Constants.APP)){
			currentApp = new App();
			currentApp.init(attributes);
			if (currentAppDefinition != null)
				currentAppDefinition.setApp(currentApp);
			else
				currentAppList.getApp().add(currentApp);
		}else if (qName.equalsIgnoreCase(Constants.CRITERIA_LIST)){
			currentCriteriaList = new CriteriaList();
			currentCriteriaList.init(attributes);
			currentDapMessage.setCriteriaList(currentCriteriaList);
		}else if (qName.equalsIgnoreCase(Constants.CRITERIA)){
			currentCriteria = new Criteria();
			currentCriteria.init(attributes);
			currentCriteriaList.getCriteria().add(currentCriteria);
		}else if (qName.equalsIgnoreCase(Constants.APP_DEFINITION)){
			currentAppDefinition = new AppDefinition();
			currentAppDefinition.init(attributes);
		}
		
		
		currentElement = true;
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (currentElement){
			currentValue = new String(ch, start, length);
			currentElement = false;
		}
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (qName.equalsIgnoreCase(Constants.COMMAND)){
			currentDapMessage.setCommand(currentValue);
		}else if (qName.equalsIgnoreCase(Constants.AGENT_NAME)){
			currentDapMessage.setAgentName(currentValue);
		}else if (qName.equalsIgnoreCase(Constants.APP_NAME)){
			currentDapMessage.setAppName(currentValue);
		}else if (qName.equalsIgnoreCase(Constants.APP_VERSION)){
			currentDapMessage.setAppVersion(currentValue);
		}else if (qName.equalsIgnoreCase(Constants.AGENT_LISTENING_PORT)){
			currentDapMessage.setAgentListeningPort(BigInteger.valueOf(Long.valueOf(currentValue)));
		}else if (qName.equalsIgnoreCase(Constants.AGENT_VERSION)){
			currentDapMessage.setAgentVersion(currentValue);
		}else if (qName.equalsIgnoreCase(Constants.PROXY_PORT)){
			currentDapMessage.setProxyPort(BigInteger.valueOf(Long.valueOf(currentValue)));
		}else if (qName.equalsIgnoreCase(Constants.PROXY_ADDRESS)){
			currentDapMessage.setProxyAddress(currentValue);
		} else if (qName.equalsIgnoreCase(Constants.RESPONSE)){
			currentDapMessage.setResponse(currentValue);
		}else if (qName.equalsIgnoreCase(Constants.REGISTRATION_ID)){
			currentDapMessage.setRegistrationId(currentValue);
		}else if (qName.equalsIgnoreCase(Constants.ACTION)){
			currentActions.getAction().add(currentValue);
		}      
	}
	
	public DapMessage getDapMessage() {
		return currentDapMessage;
	}
	
	public AppDefinition getAppDefinition() {
		return currentAppDefinition;
	}
	
}
